package simulation.threads;

import simulation.initiatives.EngineBuffer;
import common.Buffer;
import common.Constants;
import common.SimulationSettings;

public class EngineBufferThread extends EngineBuffer implements Runnable {  
  private volatile boolean threadSuspended = true;
  
  public EngineBufferThread(Buffer buffer, SimulationSettings settings) {
    super(buffer, settings);
  }
    
  public synchronized void start() {
    threadSuspended = false;
    notify();
  }
  
  public synchronized void pause() {
    threadSuspended = true;
  }
  
  public synchronized void reset() {
    pause();
    super.reset();
  } 
  
  public void run() {
    Thread thisThread = Thread.currentThread();
    
    while(true) {
      try {
        if (!threadSuspended) {
          thisThread.sleep(Constants.BUFFER_PUSH_RATE);
          super.pushToBuffer();
        }
        else {
          synchronized (this) {
            wait();
          }
        }
      }
      catch (InterruptedException e) {
        e.printStackTrace();
      }      
    }
  }
}
